package br.com.viamais.mb;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import br.com.viamais.util.MessageBundleUtil;

/**
 * <B>Project: viamais-web</B><BR>
 * 
 * Base class for all JSF Managed Beans.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Sep 18, 2011</DD></DT>
 * </DL>
 * 
 * TODO: Logger
 * 
 * @author Caio Carleto - CarSer - Sep 18, 2011
 * @version 1.0
 */
@RequestScoped
@ManagedBean
public abstract class BaseMB {
	/**
	 * Success title.
	 */
	private static final String MESSAGE_SUCESS_TITLE = MessageBundleUtil
			.getValue("manager.message.generic.sucess.title");
	/**
	 * Error title.
	 */
	private static final String MESSAGE_ERROR_TITLE = MessageBundleUtil
			.getValue("manager.message.generic.error.title");
	/**
	 * Warn title.
	 */
	private static final String MESSAGE_WARN_TITLE = MessageBundleUtil
			.getValue("manager.message.generic.warn.title");
	/**
	 * Fatal title.
	 */
	private static final String MESSAGE_FATAL_TITLE = MessageBundleUtil
			.getValue("manager.message.generic.fatal.title");

	/**
	 * Enumerator with faces messages titles.
	 * 
	 * @author eduardo.dev
	 */
	enum MessageType {
		/**
		 * Messages type.
		 */
		MESSAGE_SUCESS(1, MESSAGE_SUCESS_TITLE), MESSAGE_ERROR(2,
				MESSAGE_ERROR_TITLE), MESSAGE_WARN(3, MESSAGE_WARN_TITLE), MESSAGE_FATAL(
				4, MESSAGE_FATAL_TITLE);
		/**
		 * Index
		 */
		private int index;
		/**
		 * Title
		 */
		private String title;

		/**
		 * Constructor
		 * 
		 * @param String
		 *            : title
		 */
		private MessageType(int index, String title) {
			this.index = index;
			this.title = title;
		}

		/**
		 * @return the index
		 */
		public int getIndex() {
			return index;
		}

		/**
		 * @return the title
		 */
		public String getTitle() {
			return title;
		}
	}

	/**
	 * Faces context
	 */
	private FacesContext context;
	/**
	 * Faces message
	 */
	private FacesMessage message;
	/**
	 * Search permission.
	 */
	boolean canSearch;
	/**
	 * Edit permission
	 */
	boolean canEdit;
	/**
	 * Remove permission
	 */
	boolean canRemove;

	/**
	 * Method to reset fields within faces form.
	 */
	public abstract void reset();

	/**
	 * @return the message
	 */
	public FacesMessage getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(FacesMessage message) {
		this.message = message;
	}

	/**
	 * @return the canSearch
	 */
	public boolean isCanSearch() {
		return canSearch;
	}

	/**
	 * @param canSearch
	 *            the canSearch to set
	 */
	public void setCanSearch(boolean canSearch) {
		this.canSearch = canSearch;
	}

	/**
	 * @return the canEdit
	 */
	public boolean isCanEdit() {
		return canEdit;
	}

	/**
	 * @param canEdit
	 *            the canEdit to set
	 */
	public void setCanEdit(boolean canEdit) {
		this.canEdit = canEdit;
	}

	/**
	 * @return the canRemove
	 */
	public boolean isCanRemove() {
		return canRemove;
	}

	/**
	 * @param canRemove
	 *            the canRemove to set
	 */
	public void setCanRemove(boolean canRemove) {
		this.canRemove = canRemove;
	}

	/**
	 * @param context
	 *            the context to set
	 */
	public void setContext(FacesContext context) {
		this.context = context;
	}

	/**
	 * @return the context
	 */
	public FacesContext getContext() {
		if (this.context == null) {
			context = FacesContext.getCurrentInstance();
		}
		return context;
	}

	/**
	 * @return validation status.
	 */
	public boolean isValidationFailed() {
		return this.getContext().isValidationFailed();
	}

	/**
	 * Add a new message do Faces context.
	 * 
	 * @param MessageType
	 *            : type of message.
	 * @param String
	 *            : detail of message.
	 * @return
	 */
	protected void addFacesMessage(final MessageType type, final String detail) {
		message = new FacesMessage();
		final int index = type != null ? type.getIndex()
				: MessageType.MESSAGE_SUCESS.getIndex();
		switch (index) {
		case 2:
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			this.getContext().validationFailed();
			break;
		case 3:
			message.setSeverity(FacesMessage.SEVERITY_WARN);
			break;
		case 4:
			message.setSeverity(FacesMessage.SEVERITY_FATAL);
			this.getContext().validationFailed();
			break;
		default:
			message.setSeverity(FacesMessage.SEVERITY_INFO);
		}
		if (type != null) {
			message.setSummary(type.getTitle());
		} else {
			message.setSummary(MessageType.MESSAGE_SUCESS.getTitle());
		}
		message.setSummary(detail);
		this.getContext().addMessage(null, message);
	}
}